﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace libReadWrite
{
    public class BigEndianWriterForWifi: BigEndianWriter
    {
        private Socket socket;

        public BigEndianWriterForWifi(Socket in_socket)
        {
            this.socket = in_socket;
        }

        public override void Write(byte v)
        {
            byte[] buffer = new byte[1];
            buffer[0] = v;
            SocketController.Send(socket, buffer, 0, 1, 10000);
        }

        public override void Write(byte[] buffer)
        {
            SocketController.Send(socket, buffer, 0, buffer.Length, 10000);
        }

        public override void Write(int v)
        {
            byte[] buffer = ConvertToBigEndianBytes(BitConverter.GetBytes(v));

            //byte[] buffer = BitConverter.GetBytes(v);
            SocketController.Send(socket, buffer, 0, buffer.Length, 10000);
        }

        public override byte[] ConvertToBigEndianBytes(byte[] input)
        {   
            //70 00 00 00 --> 00 00 00 70
            
            byte[] bytes = new byte[input.Length];
            int j = 0;
            for (int i = input.Length - 1; i >= 0; i--, j++)
                bytes[i] = input[j];

            return bytes;
        }

        public override void Write(long v)
        {
            byte[] buffer = ConvertToBigEndianBytes(BitConverter.GetBytes(v));

            //byte[] buffer = BitConverter.GetBytes(v);
            SocketController.Send(socket, buffer, 0, buffer.Length, 10000);
        }
    }
}
